package com.apisix.service.controller;

import com.apisix.service.service.ApisixService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.HttpClientErrorException;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/apisix")
public class ApisixController {

    private final ApisixService apisixService;

    @Autowired
    public ApisixController(ApisixService apisixService) {
        this.apisixService = apisixService;
    }

    // 路由端点
    @GetMapping("/routes")
    public ResponseEntity<?> getRoutes() {
        try {
            List<Map<String, Object>> routes = apisixService.getRoutes();
            return ResponseEntity.ok(routes);
        } catch (HttpClientErrorException e) {
            return ResponseEntity.status(e.getStatusCode()).body(Map.of("error", e.getMessage()));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("error", "Failed to get routes: " + e.getMessage()));
        }
    }
    
    @GetMapping("/routes/{id}")
    public ResponseEntity<?> getRoute(@PathVariable String id) {
        try {
            Map<String, Object> route = apisixService.getRoute(id);
            return ResponseEntity.ok(route);
        } catch (HttpClientErrorException e) {
            return ResponseEntity.status(e.getStatusCode()).body(Map.of("error", e.getMessage()));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("error", "Failed to get route: " + e.getMessage()));
        }
    }

    @PostMapping("/routes")
    public ResponseEntity<?> createRoute(@RequestBody Map<String, Object> routeData) {
        try {
            Map<String, Object> response = apisixService.createRoute(routeData);
            return ResponseEntity.status(HttpStatus.CREATED).body(response);
        } catch (HttpClientErrorException e) {
            return ResponseEntity.status(e.getStatusCode()).body(Map.of("error", e.getMessage()));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("error", "Failed to create route: " + e.getMessage()));
        }
    }

    @PutMapping("/routes/{id}")
    public ResponseEntity<?> updateRoute(@PathVariable String id, @RequestBody Map<String, Object> routeData) {
        try {
            Map<String, Object> response = apisixService.updateRoute(id, routeData);
            return ResponseEntity.ok(response);
        } catch (HttpClientErrorException e) {
            return ResponseEntity.status(e.getStatusCode()).body(Map.of("error", e.getMessage()));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("error", "Failed to update route: " + e.getMessage()));
        }
    }

    @DeleteMapping("/routes/{id}")
    public ResponseEntity<?> deleteRoute(@PathVariable String id) {
        try {
            Map<String, Object> response = apisixService.deleteRoute(id);
            return ResponseEntity.ok(response);
        } catch (HttpClientErrorException e) {
            return ResponseEntity.status(e.getStatusCode()).body(Map.of("error", e.getMessage()));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("error", "Failed to delete route: " + e.getMessage()));
        }
    }

    // 上游端点
    @GetMapping("/upstreams")
    public ResponseEntity<?> getUpstreams() {
        try {
            List<Map<String, Object>> upstreams = apisixService.getUpstreams();
            return ResponseEntity.ok(upstreams);
        } catch (HttpClientErrorException e) {
            return ResponseEntity.status(e.getStatusCode()).body(Map.of("error", e.getMessage()));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("error", "Failed to get upstreams: " + e.getMessage()));
        }
    }
    
    @GetMapping("/upstreams/{id}")
    public ResponseEntity<?> getUpstream(@PathVariable String id) {
        try {
            Map<String, Object> upstream = apisixService.getUpstream(id);
            return ResponseEntity.ok(upstream);
        } catch (HttpClientErrorException e) {
            return ResponseEntity.status(e.getStatusCode()).body(Map.of("error", e.getMessage()));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("error", "Failed to get upstream: " + e.getMessage()));
        }
    }

    @PostMapping("/upstreams")
    public ResponseEntity<?> createUpstream(@RequestBody Map<String, Object> upstreamData) {
        try {
            Map<String, Object> response = apisixService.createUpstream(upstreamData);
            return ResponseEntity.status(HttpStatus.CREATED).body(response);
        } catch (HttpClientErrorException e) {
            return ResponseEntity.status(e.getStatusCode()).body(Map.of("error", e.getMessage()));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("error", "Failed to create upstream: " + e.getMessage()));
        }
    }

    @PutMapping("/upstreams/{id}")
    public ResponseEntity<?> updateUpstream(@PathVariable String id, @RequestBody Map<String, Object> upstreamData) {
        try {
            Map<String, Object> response = apisixService.updateUpstream(id, upstreamData);
            return ResponseEntity.ok(response);
        } catch (HttpClientErrorException e) {
            return ResponseEntity.status(e.getStatusCode()).body(Map.of("error", e.getMessage()));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("error", "Failed to update upstream: " + e.getMessage()));
        }
    }

    @DeleteMapping("/upstreams/{id}")
    public ResponseEntity<?> deleteUpstream(@PathVariable String id) {
        try {
            Map<String, Object> response = apisixService.deleteUpstream(id);
            return ResponseEntity.ok(response);
        } catch (HttpClientErrorException e) {
            return ResponseEntity.status(e.getStatusCode()).body(Map.of("error", e.getMessage()));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("error", "Failed to delete upstream: " + e.getMessage()));
        }
    }

    // 服务端点
    @GetMapping("/services")
    public ResponseEntity<?> getServices() {
        try {
            List<Map<String, Object>> services = apisixService.getServices();
            return ResponseEntity.ok(services);
        } catch (HttpClientErrorException e) {
            return ResponseEntity.status(e.getStatusCode()).body(Map.of("error", e.getMessage()));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("error", "Failed to get services: " + e.getMessage()));
        }
    }
    
    @GetMapping("/services/{id}")
    public ResponseEntity<?> getService(@PathVariable String id) {
        try {
            Map<String, Object> service = apisixService.getService(id);
            return ResponseEntity.ok(service);
        } catch (HttpClientErrorException e) {
            return ResponseEntity.status(e.getStatusCode()).body(Map.of("error", e.getMessage()));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("error", "Failed to get service: " + e.getMessage()));
        }
    }

    @PostMapping("/services")
    public ResponseEntity<?> createService(@RequestBody Map<String, Object> serviceData) {
        try {
            Map<String, Object> response = apisixService.createService(serviceData);
            return ResponseEntity.status(HttpStatus.CREATED).body(response);
        } catch (HttpClientErrorException e) {
            return ResponseEntity.status(e.getStatusCode()).body(Map.of("error", e.getMessage()));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("error", "Failed to create service: " + e.getMessage()));
        }
    }

    @PutMapping("/services/{id}")
    public ResponseEntity<?> updateService(@PathVariable String id, @RequestBody Map<String, Object> serviceData) {
        try {
            Map<String, Object> response = apisixService.updateService(id, serviceData);
            return ResponseEntity.ok(response);
        } catch (HttpClientErrorException e) {
            return ResponseEntity.status(e.getStatusCode()).body(Map.of("error", e.getMessage()));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("error", "Failed to update service: " + e.getMessage()));
        }
    }

    @DeleteMapping("/services/{id}")
    public ResponseEntity<?> deleteService(@PathVariable String id) {
        try {
            Map<String, Object> response = apisixService.deleteService(id);
            return ResponseEntity.ok(response);
        } catch (HttpClientErrorException e) {
            return ResponseEntity.status(e.getStatusCode()).body(Map.of("error", e.getMessage()));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("error", "Failed to delete service: " + e.getMessage()));
        }
    }
    
    // 消费者端点
    @GetMapping("/consumers")
    public ResponseEntity<?> getConsumers() {
        try {
            List<Map<String, Object>> consumers = apisixService.getConsumers();
            return ResponseEntity.ok(consumers);
        } catch (HttpClientErrorException e) {
            return ResponseEntity.status(e.getStatusCode()).body(Map.of("error", e.getMessage()));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("error", "Failed to get consumers: " + e.getMessage()));
        }
    }
    
    @GetMapping("/consumers/{id}")
    public ResponseEntity<?> getConsumer(@PathVariable String id) {
        try {
            Map<String, Object> consumer = apisixService.getConsumer(id);
            return ResponseEntity.ok(consumer);
        } catch (HttpClientErrorException e) {
            return ResponseEntity.status(e.getStatusCode()).body(Map.of("error", e.getMessage()));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("error", "Failed to get consumer: " + e.getMessage()));
        }
    }
} 